package com.keyancompany.zhongbaopingtai.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.TalkAdapter;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.ShowTalk;
import com.keyancompany.zhongbaopingtai.model.TalkComment;
import com.keyancompany.zhongbaopingtai.model.TalkList;
import com.keyancompany.zhongbaopingtai.utils.DebugLog;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.keyancompany.zhongbaopingtai.utils.T;

import java.util.List;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 讨论详情
 *
 * @author xihe
 */
public class DiscussDetailActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.team_name)
    TextView title;
    @Bind(R.id.author)
    TextView author;
    @Bind(R.id.talk_time)
    TextView talkTime;
    @Bind(R.id.task_content)
    TextView taskContent;
    @Bind(R.id.list)
    RecyclerView recyclerView;

    @BindString(R.string.discuss_detail)
    String discussTitle;

    TalkList talkContent;
    String teamCode;
    @Bind(R.id.edit_talk)
    EditText editTalk;
    @Bind(R.id.add_talk)
    Button addTalk;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_discuss_detail;
    }

    @Override
    protected int getMenuId() {
        return R.menu.add_talk_menu;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, discussTitle);
    }

    @Override
    protected void setUpViews() {

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.add_talk) {

                    if (isNetvaiable()) {
                        if (talkContent != null) {
                            TeamAPI.saveTeamTalk(teamCode, talkContent.getTalkTitle(),
                                    talkContent.getTalkContent(), Constants.TALKING, DiscussDetailActivity.this,
                                    getLoading(R.string.watting_hint));
                        }
                    }
                    return true;
                }
                return false;
            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        talkContent = (TalkList) getIntent().getSerializableExtra(Constants.TALK_CONTENT);
        teamCode = SPUtils.getComment(mContext).getString(Constants.TEAM_CODE, null);

        initData();
    }

    private void initData() {
        if (isNetvaiable()) {
            TeamAPI.showTalk(teamCode, talkContent.getId(), DiscussDetailActivity.this,
                    getLoading(R.string.watting_hint));
        }
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        switch (type) {
            case TeamAPI.SAVE_TEAM_TALK_REPLY_MARK://发表评论
                DebugLog.i("发表评论请求成功 ");
                if (validateJson(json) != null) {
                    DebugLog.i(json);
                    BaseModel base = JSONObject.parseObject(json, BaseModel.class);
                    T.showShort(mContext, base.msg);
                }

                editTalk.setText("");
                break;
            case TeamAPI.SHOW_TALK_MARK:
                if (validateJson(json) != null) {
                    BaseModel base = JSONObject.parseObject(json, BaseModel.class);
                    DebugLog.d("讨论详情: " + json);
                    ShowTalk showTalk = JSONObject.parseObject(base.data, ShowTalk.class);
                    if (base.result.equals(Constants.SUCCESS)) {
                        author.setText("发布者：" + showTalk.getTalkName());//发布者

                        if (showTalk.getTalk() != null) {
                            title.setText("讨论标题：" + showTalk.getTalk().getTalkTitle());
                            taskContent.setText(showTalk.getTalk().getTalkContent());
                            talkTime.setText("发表时间：" + showTalk.getTalk().getTalkTime());
                        } else {
                            return;
                        }

                        if (showTalk.getList().size() > 0) {//
                            List<TalkComment> comments = showTalk.getList();

                            DebugLog.i("Reply list size: " + comments.size());
                            for (TalkComment comment: comments) {
                                DebugLog.i("Comment: " + comment.getReplyContent() + "    " + comment.getReplyTime());
                            }
                            recyclerView.setAdapter(new TalkAdapter(showTalk.getList()));

                        } else {
                            T.showShort(mContext, "暂无讨论的评论信息！");
                            return;
                        }

                    } else {
                        T.showShort(mContext, base.msg);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        T.showShort(mContext, error);
    }



    @OnClick(R.id.add_talk)
    public void onClick() {
        if (isNetvaiable()) {
            String replyContent = editTalk.getText().toString();
            if (replyContent != null) {
                TeamAPI.saveTeamTalkReply(teamCode,
                        replyContent, talkContent.getId(), DiscussDetailActivity.this,
                        getLoading(R.string.watting_hint));
            }
        }
    }
 }
